package cn.flying.cloud.demo.endofday.steps.s1100;

import cn.flying.cloud.task.core.batch.configuration.BatchProperties;
import cn.flying.cloud.demo.endofday.model.Employee;

import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author: admin
 * @date: 2024年01月12日 15:33
 * @version: 1.0
 */
@Component
public class R1100Reader extends FlatFileItemReader<Employee> {

    public R1100Reader(BatchProperties properties) {
        //设置编码方式
        setEncoding("UTF-8");
        //设置字段映射关系
        DefaultLineMapper<Employee> lineMapper = new DefaultLineMapper<>();

        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setDelimiter(",");

        Employee employee = new Employee();
        Field[] fields = employee.getClass().getDeclaredFields();
        String[] names = Arrays.stream(fields).map(f -> f.getName()).toArray(String[]::new);
        tokenizer.setNames(names);

        lineMapper.setLineTokenizer(tokenizer);

        BeanWrapperFieldSetMapper<Employee> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
        fieldSetMapper.setTargetType(Employee.class);
        lineMapper.setFieldSetMapper(fieldSetMapper);

        setLineMapper(lineMapper);
        //设置跳过行数
        setLinesToSkip(1);
        //读取的文件
        setResource(new FileSystemResource(properties.getPath() + "/input/inputData1.txt"));
    }
}
